﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace DAL
{
    public class PostsDataModel
    {
        public List<Store_Post_ViewWishListOfUser_Result> getWishListOfUser(string username)
        {
            HomestayWebDBEntities en = new HomestayWebDBEntities();
            return en.Store_Post_ViewWishListOfUser(username).ToList();
        }
        public List<Store_HomeStayType_GetNameHomeStayType_Result> getHomestayname(int postID)
        {
            return ent.Store_HomeStayType_GetNameHomeStayType(postID).ToList();
        }
        public bool deleteWishListOfUser(int postID,string username)
        {
            HomestayWebDBEntities ent = new HomestayWebDBEntities();
            //string proc = "sp_Post_DeleteWishListItemOfUser";
            int i = ent.ExecuteStoreCommand("exec Store_Post_DeleteWishListItemOfUser @PostID={0},@Username={1} ", new object[2] { postID, username });
            if(i>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public Store_Post_GetDetailOfPost_Result GetDetailOfPost(int postID)
        {
            HomestayWebDBEntities ent = new HomestayWebDBEntities();
            Store_Post_GetDetailOfPost_Result l = ent.Store_Post_GetDetailOfPost(postID).FirstOrDefault();
            if (l != null)
            {
                return l;
            }

            return null;
        }

        #region Hoangdx - Code Posts Function

        HomestayWebDBEntities ent = new HomestayWebDBEntities();

        #region Code New Post
        public void CreatePost(Post postDB, House houseDB, List<String> ListImageFile, string[] isChecked)
        {
            bool isAvatar = true;
            House houseDTO = new House();
            Post postDTO = new Post();

            postDTO = postDB;

            postDTO.House = houseDB;
            //gen date time
            postDTO.TimeCreated = DateTime.Now;
            foreach (string fileImage in ListImageFile)
            {
                Image imageDTO = new Image();
                imageDTO.ImageName = fileImage;
                //imageDTO.IsPosAvatar = isAvatar;
                postDTO.Images.Add(imageDTO);
                postDTO.PostAvatar = fileImage;
                isAvatar = false;
            }

            if (isChecked != null)
            {
                foreach (string chk in isChecked)
                {
                    Tourism tourismDTO = new Tourism();
                    tourismDTO.HomeStayTypeID = Int32.Parse(chk);
                    postDTO.House.Tourisms.Add(tourismDTO);
                }
            }

            ent.Posts.AddObject(postDTO);
            ent.SaveChanges();

        }

        public List<Post_ListHomeStayTypes_View> GetListHomeStayTypes()
        {
            List<Post_ListHomeStayTypes_View> listHomeStayTypes = ent.Store_Post_GetListHomeStayTypes().ToList<Post_ListHomeStayTypes_View>();
            return listHomeStayTypes;
        }
        #endregion

        #region Code Edit Post

        public Post_GetEditPost_View GetEditPost(int postID)
        {
            Post_GetEditPost_View postEdit = ent.Store_Posts_GetEditPost(postID).FirstOrDefault();
            return postEdit;
        }

        public void UpdatePost(Post postDB, House houseDB)
        {
            Post post = null;

            post = (from s in ent.Posts.Include("House")
                    where s.PostID == postDB.PostID
                    select s).FirstOrDefault<Post>();

            post.Title = postDB.Title;
            post.Content = postDB.Content;
            post.ContactName = postDB.ContactName;
            post.MobilePhone = postDB.MobilePhone;
            post.TelePhone = postDB.TelePhone;
            post.ContactEmail = postDB.ContactEmail;
            post.IsAvailable = postDB.IsAvailable;
            post.TimeCreated = postDB.TimeCreated;
            post.PostStatus = 0;
            post.Username = postDB.Username;

            post.House.AddressDetail = houseDB.AddressDetail;
            post.House.TypeOfHouseID = houseDB.TypeOfHouseID;
            post.House.Area = houseDB.Area;
            post.House.HasGara = houseDB.HasGara;
            post.House.HasKitchen = houseDB.HasKitchen;
            post.House.HasGarden = houseDB.HasGarden;
            post.House.PricePerDay = houseDB.PricePerDay;
            post.House.PricePerWeek = houseDB.PricePerWeek;
            post.House.PricePerMonth = houseDB.PricePerMonth;
            post.House.NumSittingRoom = houseDB.NumSittingRoom;
            post.House.NumBadroom = houseDB.NumBadroom;
            post.House.Storey = houseDB.Storey;
            post.House.NumOfWC = houseDB.NumOfWC;
            post.House.Ordinate = houseDB.Ordinate;



            ent.ObjectStateManager.ChangeObjectState(post, System.Data.EntityState.Unchanged);
            ent.Posts.Attach(post);
            ent.ObjectStateManager.ChangeObjectState(post, System.Data.EntityState.Modified);
            ent.SaveChanges();

        }

        public int ChangeAvaliable(int postID)
        {
            try
            {
                return ent.Store_Posts_ChangeAvaliable(postID);
            }
            catch (Exception ex)
            {
                throw ex;
                //return false;
            }
        }

        public void DeletePost(int postId)
        {
            ent.Store_Posts_DeletePost(postId);
        }

        #endregion

        #endregion

        public List<Store_Post_GetPostOfUser_Result> GetPostOfUser(string username)
        {
            HomestayWebDBEntities ent = new HomestayWebDBEntities();
            List<Store_Post_GetPostOfUser_Result> l = ent.Store_Post_GetPostOfUser(username).ToList();
            return l;
        }
        public List<Store_Likes_SelectTopPost_Result> GetTopLikePost()
        {
            HomestayWebDBEntities en = new HomestayWebDBEntities();
            return ent.Store_Likes_SelectTopPost().ToList();
        }


        #region Khoana - ManagePosts GetDetailOfPost
        public List<Store_ManagePosts_GetDetailOfPost_Result> ManagePostsGetDetailOfPost (int postId)
        {
           HomestayWebDBEntities ent = new HomestayWebDBEntities();
            List<Store_ManagePosts_GetDetailOfPost_Result> list = ent.Store_ManagePosts_GetDetailOfPost(postId).ToList();
            return list;
        }
        #endregion

        #region Khoana - ManagePost AcceptPost
        public int  AcceptPost (int postId, string reviewBy)
        {
            try
            {
                return ent.Store_ManagePosts_AcceptPost(postId, reviewBy);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Khoana - ManagePost AcceptPost
        public int RejectPost(int postId, string reviewBy)
        {
            try
            {
                return ent.Store_ManagePosts_RejectPost(postId, reviewBy);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region khoana -show number of valid posts in province
        public List<Store_NoHouseInProvince_Result> GetNoHouseInProvince ()
        {
            HomestayWebDBEntities ent = new HomestayWebDBEntities();
            List<Store_NoHouseInProvince_Result> listresult = ent.Store_NoHouseInProvince().ToList();
            return listresult;
        }
        #endregion
    }
}
